//驗證器
var validator = {
    cache: [],
    New: function () {
        this.cache = []
    },
    Add: function (dom, rule, errMsg,errDom) {
        var arr = rule.split(':');
        var reg = arr[0];

        arr.shift();
        arr.unshift(dom.value);
        arr.push(errMsg);
        this.cache.push({dom:errDom,fun:function () {
            return metcher[reg].apply(dom, arr);
        }});
    },
    Run: function () {
        for (var i = 0; i < this.cache.length; i++) {
            var msg = this.cache[i].fun();
            if (msg){
                this.cache[i].dom.innerText=msg;
                return msg;
            }
            this.cache[i].dom.innerText='';
        }
    }
}

//匹配規則
var metcher = {
    len: function (val, min, max, errMsg) {
        if (val.length < min || val.length > max) {
            return errMsg;
        }
    },
    minlen: function (val, min, errMsg) {
        if (val.length < min) {
            return errMsg;
        }
    },
    maxlen: function (val, max, errMsg) {
        if (val.length > max) {
            return errMsg;
        }
    },
    //富士康工号
    fno: function (val, errMsg) {
        var patt = new RegExp(/^(([A-Z0-9a-z]|[\u4e00-\u9fa5]{1,4}){4,10})|(\d{18})$/);
        if (!patt.test(val)) {
            return errMsg;
        }
    },
    //富士康邮箱
    fmail: function (val, errMsg) {
        var patt = new RegExp(/^([a-zA-Z]|[0-9])(\w|\-)+@(mail.foxconn.com)|(foxconn.com)$/);
        if (!patt.test(val)) {
            return errMsg;
        }
    },
    //富士康分机
    ftel: function (val, errMsg) {
        var patt = new RegExp(/(^\d{5,6}$)|(^\d{3}-\d{5,6}$)|(^\d{8,9}$)/);
        if (!patt.test(val)) {
            return errMsg;
        }
    },
    //邮箱
    mail: function (val, errMsg) {
        var patt = new RegExp(/^([a-zA-Z0-9]+[_|_|\-|.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|_|.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,6}$/);
        if (!patt.test(val)) {
            return errMsg;
        }
    },
    //身份证号
    sid: function (val, errMsg) {
        var patt = new RegExp(/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/);
        if (!patt.test(val)) {
            return errMsg;
        }
    },
    //手机号(大陆，香港，澳门)
    mobile: function (val, errMsg) {
        var patt = new RegExp(/(^\d{11}$)|(^([6|9])\d{7}$)|(^[6]([8|6])\d{5})/);
        if (!patt.test(val)) {
            return errMsg;
        }
    },
    //日期：2021-01-01
    date: function (val, errMsg) {
        var patt = new RegExp(/^\d{4}\-(0[1-9]|1[0-2])\-(0[1-9]|[1-2][0-9]|3[0-1])$/);
        if (!patt.test(val)) {
            return errMsg;
        }
    }
}